﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class Material : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string dateNow = ConvestToDate((DateTime.Now).ToShortDateString());
                Session["cStart"] = "0001-01-01";
                Session["cEnd"] = dateNow;
            }
        }
        public string ConvestToDate(string s)
        {
            string day = "";
            string mount = "";
            string years = "";

            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) > 2300)
                {
                    years = (Convert.ToInt32(years) - 543).ToString();
                }

                return (years + "-" + mount + "-" + day);
            }
            catch (Exception)
            {

            }
            return (years + "-" + mount + "-" + day);
        }
        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            string Material_ID = e.CommandArgument.ToString();
            Session["sOrder_Material_ID"] = Material_ID;
            Response.Redirect("MeterialList.aspx");
        }

        protected void ASPxButton8_Click(object sender, EventArgs e)
        {
            string a = ConvestToDate((deStart0.Date).ToShortDateString());
            string b = ConvestToDate((edEnd0.Date).ToShortDateString());
            Session["cStart"] = a;
            Session["cEnd"] = b;
            SqlMainGV.Select(DataSourceSelectArguments.Empty);
            gvMain.DataBind();
        }
        public string ConvestToDateN(string s)
        {
            string day = "";
            string mount = "";
            string years = "";

            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) > 2300)
                {
                    years = (Convert.ToInt32(years) - 543).ToString();
                }

                return (day + "-" + mount + "-" + years);
            }
            catch (Exception)
            {

            }
            return (day + "-" + mount + "-" + years);
        }

        protected void SqlMainGV_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@sStart"].Value = Session["cStart"].ToString();
            e.Command.Parameters["@sEnd"].Value = Session["cEnd"].ToString();
        }

        protected void ASPxButton11_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart0.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd0.Date).ToShortDateString());
            if (a == "1-1-0001" && b == "1-1-0001")
            {
                a = " ทั้งหมด ";
                b = " ทั้งหมด ";
            }
            ASPxGridViewExporter1.ReportHeader = "\t\tรายงานการสั่งซื้อวัตถุดิบ\n\t ตั้งแต่วันที่:" + a + " ถึงวันที่:" + b + "\n";
            ASPxGridViewExporter1.WritePdfToResponse();
        }

        protected void ASPxButton13_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart0.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd0.Date).ToShortDateString());
            if (a == "1-1-0001" && b == "1-1-0001")
            {
                a = " ทั้งหมด ";
                b = " ทั้งหมด ";
            }
            ASPxGridViewExporter1.ReportHeader = "\t\tรายงานการสั่งซื้อวัตถุดิบ\n\t ตั้งแต่วันที่:" + a + " ถึงวันที่:" + b + "\n";
            ASPxGridViewExporter1.WriteXlsToResponse();
        }

        protected void ASPxButton12_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd.Date).ToShortDateString());
            if (a == "1-1-0001" && b == "1-1-0001")
            {
                a = " ทั้งหมด ";
                b = " ทั้งหมด ";
            }
            ASPxGridViewExporter2.ReportHeader = "\t\tรายงานการซื้อวัตถุดิบ\n\t ตั้งแต่วันที่:" + a + " ถึงวันที่:" + b + "\n";
            ASPxGridViewExporter2.WritePdfToResponse();
        
        }

        protected void ASPxButton14_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd.Date).ToShortDateString());
            if (a == "1-1-0001" && b == "1-1-0001")
            {
                a = " ทั้งหมด ";
                b = " ทั้งหมด ";
            }
            ASPxGridViewExporter2.ReportHeader = "\t\tรายงานการซื้อวัตถุดิบ\n\t ตั้งแต่วันที่:" + a + " ถึงวันที่:" + b + "\n";
            ASPxGridViewExporter2.WriteXlsToResponse();
        
        }

        protected void ASPxButton7_Click(object sender, EventArgs e)
        {
            string a = ConvestToDate((deStart.Date).ToShortDateString());
            string b = ConvestToDate((edEnd.Date).ToShortDateString());
            Session["cStart"] = a;
            Session["cEnd"] = b;
            SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            ASPxGridView1.DataBind();
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@sStart"].Value = Session["cStart"].ToString();
            e.Command.Parameters["@sEnd"].Value = Session["cEnd"].ToString();
        }

        protected void ASPxButton9_Click(object sender, EventArgs e)
        {
            Panel5.Visible = true;
            Panel2.Visible = false;
        }

        protected void ASPxButton10_Click(object sender, EventArgs e)
        {
            Panel5.Visible = false;
            Panel2.Visible = true;
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            string dateNow = ConvestToDate((DateTime.Now).ToShortDateString());
            Session["cStart"] = "0001-01-01";
            Session["cEnd"] = dateNow;
            gvMain.DataBind();
            ASPxGridView1.DataBind();
        }
    }
}